﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SoulKeeper
{
	public class Menu
	{
        GameWindow Window;
        ContentManager Content;
        Button newgame;
        Button credits;
        Button quit;
        Button instruction;
        int MainMenuNumber;
        //int SubMenuNumber;
       
       // SpriteFont arial36;

        public Menu(ContentManager content, GameWindow Window)
        {
            this.Content = content;
            this.Window = Window;
            newgame = new Button(content.Load<Texture2D>("bg/MENU/newBT"), new Vector2(175, 110), Window);
            credits = new Button(content.Load<Texture2D>("bg/MENU/creditos"), new Vector2(175, 175), Window);
            instruction = new Button(content.Load<Texture2D>("bg/MENU/instruction"), new Vector2(175, 250), Window);
            quit = new Button(content.Load<Texture2D>("bg/MENU/quit"), new Vector2(175, 325), Window);
            
            MainMenuNumber = 0;
           // arial36 = Content.Load<SpriteFont>("Arial36");
        }

        public void Update()
        {
            howMenu();
            Window.Title = MainMenuNumber.ToString();

            if (MainMenuNumber == 0)
            {
                newgame.setAnimationIndex((int)Button.animIndexes.onThis);
                if (checkKeySpaceDown())
                {
                    Engine.current_state = Engine.GameStates.IN_GAME;
                }
                              
            }
            else
            {
               newgame.setAnimationIndex((int)Button.animIndexes.offThis);
            }
            if (MainMenuNumber == 1)
            {
                credits.setAnimationIndex((int)Button.animIndexes.onThis);
                if (checkKeySpaceDown())
                {
                    Engine.current_state = Engine.GameStates.CREDITS;
                }

            }
            else
            {
                credits.setAnimationIndex((int)Button.animIndexes.offThis);
            }
            if (MainMenuNumber == 2)
            {
                instruction.setAnimationIndex((int)Button.animIndexes.onThis);
                if (checkKeySpaceDown())
                {
                    Engine.current_state = Engine.GameStates.INSTRUCTION;
                }

            }
            else
            {
                instruction.setAnimationIndex((int)Button.animIndexes.offThis);
            }
            if (MainMenuNumber == 3)
            {
                quit.setAnimationIndex((int)Button.animIndexes.onThis);
                if (checkKeySpaceDown())
                {
                    Game1.EndGame();
                }

            }
            else
            {
                quit.setAnimationIndex((int)Button.animIndexes.offThis);
            }
           
           
           
            
           
        }//Update

        public void Draw()
        {
            //Engine.spriteBatch.DrawString(arial36, "MENU", new Vector2((Window.ClientBounds.Width / 2 - Window.ClientBounds.Width / 3), Window.ClientBounds.Height / 2), Color.White);
            newgame.Draw(Engine.gameTime, Engine.spriteBatch,true);
            credits.Draw(Engine.gameTime, Engine.spriteBatch,true);
            instruction.Draw(Engine.gameTime, Engine.spriteBatch, true);
            quit.Draw(Engine.gameTime, Engine.spriteBatch,true);
        }
        
        /// <summary>
        /// Retorna uma bool true se o teclado estiver apertado , e false se não
        /// </summary>
        /// <returns>True or Flase</returns>
        public bool checkKeySpaceDown()
        {
            if ((!(Engine.last_keyboard.IsKeyDown(Keys.Space))) && (Engine.keyboard.IsKeyDown(Keys.Space)))
            {
               return true;
            }//CheckPressSpace
            return false;
        }

        /// <summary>
        /// Checka em qual botão o jogador esta selecionado e atualiza qual ele esta
        /// </summary>
       public void howMenu()
        {
            if ((!(Engine.last_keyboard.IsKeyDown(Keys.Down))) && (Engine.keyboard.IsKeyDown(Keys.Down)))
            {
                if (MainMenuNumber < 3) { MainMenuNumber++; }
                else
                {
                    MainMenuNumber = 0;
                }

            }
            if ((!(Engine.last_keyboard.IsKeyDown(Keys.Up))) && (Engine.keyboard.IsKeyDown(Keys.Up)))
            {
                if (MainMenuNumber > 0) { MainMenuNumber--; }
                else
                {
                    MainMenuNumber = 3;
                }
            }

        }
       
       

	}
}
